/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include ".\mskin.h"

//---------------------------------------------------------------------------

MSkin::MSkin(void)
{
	Initialize();
}

MSkin::~MSkin(void)
{
}

//---------------------------------------------------------------------------

void MSkin::Initialize(void)
{
	thePath.Empty();

	backColor = RGB(0x10, 0x00, 0x10);
	backImage.Destroy();
	backImageMode = __msbimCenter;

	moveImage.Destroy();
	moveImgPos.Initialize();

	sizeImage.Destroy();
	sizeImgPos.Initialize();

	cwBackColor = RGB(50, 80, 200);
	cwTransparency = 30;

	titleColor = RGB(0xFF, 0xFF, 0x00);
	titleFontName = _T("Arial");
	titleFontSize = 8;
	titlePos.Initialize();

	textColor = RGB(0xFF, 0xFF, 0x00);
	textFontName = _T("Arial");
	textFontSize = 8;

	rgnWindow.Initialize();
}

//---------------------------------------------------------------------------

bool MSkin::InitializeToClientOnly(int ddIn)
{
	Initialize();
	return rgnWindow.SetDefaultClientRegion(ddIn);
}

//---------------------------------------------------------------------------

COLORREF MSkin::ColorFromString(CString strIn)
{
	CString str;
	BYTE rr, gg, bb;

	if(strIn.GetLength()!=11)
		return RGB(0xFF, 0, 0);

	str = strIn.Left(3);
	rr  = (BYTE)_tstoi(str);
	str = strIn.Mid(4,3);
	gg  = (BYTE)_tstoi(str);
	str = strIn.Right(3);
	bb  = (BYTE)_tstoi(str);

	return RGB(rr, gg, bb);
}

int MSkin::IntFromString(CString strIn)
{
	return _tstoi(strIn);
}

bool MSkin::LoadTheImage(CImage* imgIn, CString strIn)
{
	CString str;
	HRESULT res;

	if(imgIn==NULL) return false;

	str  = thePath;
	str += strIn;

	imgIn->Destroy();

	try{
		res = imgIn->Load(str);
	}
	catch(...){
		res = -1;
	}
	if(FAILED(res)) return false;

	int pitch;
	BYTE* bits;
	BYTE alfa;
	pitch = imgIn->GetPitch();
	bits = (BYTE*)imgIn->GetBits();

	alfa = bits[0];
	alfa = bits[1];
	alfa = bits[2];
	alfa = bits[3];
	alfa = bits[4];
	alfa = bits[5];

	return true;
}

bool MSkin::PosFromString(RgnPoint* point, CString strIn)
{
	CString str;
	int aa, bb, cc, dd;

	if(point==NULL) return false;

	point->Initialize();

	if(strIn.GetLength()!=23)
		return true;

	str = strIn.Left(5);
	aa  = _tstoi(str);
	str = strIn.Mid(6, 5);
	bb  = _tstoi(str);
	str = strIn.Mid(12, 5);
	cc  = _tstoi(str);
	str = strIn.Right(5);
	dd  = _tstoi(str);

	return point->Set(aa, bb, cc, dd);
}

bool MSkin::ProcessLine(char* buf)
{
	CString str, strId;
	int pos;

	if(buf==NULL){
		// nothing to process
		return true;
	}

	str = buf;
	pos = str.Find('=');
	if(pos==-1){
		// invalid line
		return false;
	}

	strId = str.Left(pos);
	strId = strId.Trim();

	str = str.Mid(pos+1);
	str = str.Trim();

	// main window background settings
	if(strId.CompareNoCase("BackColor")==0){
		backColor = ColorFromString(str);
		return true;
	}
	if(strId.CompareNoCase("BackImage")==0){
		if(str.GetLength()>0)
			return LoadTheImage(&backImage, str);
		return true;
	}
	if(strId.CompareNoCase("BackMode")==0){
		pos = IntFromString(str);
		if(pos<0 || pos>2) pos = 0;
		backImageMode = (MSkinBackImgMode)pos;
		return true;
	}

	// the settings for the "move" button
	if(strId.CompareNoCase("MoveImage")==0){
		if(str.GetLength()>0)
			return LoadTheImage(&moveImage, str);
		return true;
	}
	if(strId.CompareNoCase("MoveImgPos")==0){
		return PosFromString(&moveImgPos, str);
	}

	// the settings for the "size" button
	if(strId.CompareNoCase("SizeImage")==0){
		if(str.GetLength()>0)
			return LoadTheImage(&sizeImage, str);
		return true;
	}
	if(strId.CompareNoCase("SizeImgPos")==0){
		return PosFromString(&sizeImgPos, str);
	}

	// child window background settings
	if(strId.CompareNoCase("CWBackColor")==0){
		cwBackColor = ColorFromString(str);
		return true;
	}
	if(strId.CompareNoCase("CWTransp")==0){
		cwTransparency = IntFromString(str);
	}

	// child window title setting
	if(strId.CompareNoCase("TitleColor")==0){
		titleColor = ColorFromString(str);
		return true;
	}
	if(strId.CompareNoCase("TitleFont")==0){
		titleFontName = str;
		return true;
	}
	if(strId.CompareNoCase("TitleSize")==0){
		titleFontSize = IntFromString(str);
		if(titleFontSize>4) return true;
		return false;
	}
	if(strId.CompareNoCase("TitlePos")==0){
		return PosFromString(&titlePos, str);
	}

	// child window text settings
	if(strId.CompareNoCase("TextColor")==0){
		textColor = ColorFromString(str);
		return true;
	}
	if(strId.CompareNoCase("TextFont")==0){
		textFontName = str;
		return true;
	}
	if(strId.CompareNoCase("TextSize")==0){
		textFontSize = IntFromString(str);
		if(textFontSize>4) return true;
		return false;
	}

	// child window region definition
	if(strId.CompareNoCase("RgnWndDef")==0){
		strId  = thePath;
		strId += str;
		return rgnWindow.LoadFromFile(strId);
	}

	// unknown directive

	return true;
}

bool MSkin::LoadFromFile(CString pathNameIn, CString fileNameIn)
{
	CString fullFileName;
	BOOL res;
	char buffer[1025];
	LPTSTR sres;
	bool status;
	CString str;

	Initialize();

	fullFileName  = pathNameIn;
	fullFileName += _T("\\");
	fullFileName += fileNameIn;

	res = theFile.Open(fullFileName, CFile::modeRead);
	if(!res) return false;

	thePath  = pathNameIn;
	thePath += _T("\\");

	status = true;
	sres = &buffer[0];
	while(sres!=NULL && status){
		memset(buffer, 0, 1025);
		sres = theFile.ReadString(buffer, 1024);
		if(sres!=NULL){
			str = buffer;
			str = str.Trim();
			str = str.Left(1);
			if(str.GetLength()==0){
				// empty line, ignore
			}
			else{
				if(str.CompareNoCase("#")==0){
					// comment, ignore
				}
				else{
					// process this line
					status = ProcessLine(buffer);
				}
			}
		}
	}

	theFile.Close();

	return status;
}

//---------------------------------------------------------------------------
